// CLass Name UIAlertView

// Initializing UIAlertView 
// there are 5 parameters passed to constructor below are the details of parameters
// 1.initWithTitle				: it is first parameter. it will take title string and set UIAlertView title
// 2.message       				: it will take message string and set Message in UiAlertView
// 3.delegate 	   				: setting delegate.
// 4.cancelButtonTitle			: it will take cancel button title string which should be localized string
// 5.otherButtonTitles			: it will take ok button title string which should be localized string

// setter functions
// there are 9 setter function through which you can set button text, title text and message text colors.
// it is also allow you to set background and button normal and highlighted images path
// you can also set type of your alertView and type detail is given below

// there are two type of alert view
// 1."ok" which means UIAlertView with ok button only
// 2."okwithcancel" means UIAlertView with ok and cancel button both 
// by default it takes type "ok"

//Default type for alert window
var kALERT_WINDOW_TYPE = "ok"

//AlertWindow button image default paths
var kALERT_WINDOW_BACKGROUND  = "./Content/images/gamelayer/controllayer/mainmenu/main-menu-background.png"

// Ok buttons
var kALERT_WINDOW_OK_NORMAL = "./Content/images/gamelayer/controllayer/mainmenu/close-button-normal.png"
var kALERT_WINDOW_OK_SELECTED = "./Content/images/gamelayer/controllayer/mainmenu/close-button-pressed.png"

// Cancel buttons
var kALERT_WINDOW_CANCEL_NORMAL ="./Content/alert-cancel-normal.png"
var kALERT_WINDOW_CANCEL_SELECTED ="./Content/alert-cancel-selected.png"


exports.GAUIAlertView = UI.View.subclass({
	
	initialize: function($super,initWithTitle,message,delegate,cancelButtonTitle,otherButtonTitles) 
	{
		$super();
		this.w							= null;
		this.h							= null;
		this._tagValue					= null;
		
		this._title 					= null;
		this._message 					= null;
		this._delegate 					= null;
		this.cancelButtonTitle 			= null;
		this._otherButtonTitles			= null;
		
		this._okButton					= null;
		this._cancelButton 				= null;
		this._messageLabel 	    		= null;
		this._titleLabel	    		= null;
		this._backgroundImage 			= null;
		this._type						= null;
		
		this._backgroundImagePath		= null;
		this._okImageNormalPath 		= null;
		this._okImageSelectedPath 		= null;
		this._cancelImageNormalPath		= null;
		this._cancelImageSelectedPath	= null;
		
		this._buttonsColor				=null;
		this._titleColor				=null;
		this._messageColor				=null;
		
		
		// initializing data members with parameters 
		if (initWithTitle == null) {
			initWithTitle = "Message";
			}
		
		if (initWithTitle.length > 11) {
			this._title = initWithTitle.substring(0, 10);
			} 
		else 
		{
			this._title = initWithTitle;
		}
		
		this._message = message;
		this._delegate = delegate;
		this.cancelButtonTitle = cancelButtonTitle;
		this._otherButtonTitles= otherButtonTitles;	
		
		// setting images default path
		this._backgroundImagePath		=kALERT_WINDOW_BACKGROUND;
		this._okImageNormalPath			=kALERT_WINDOW_OK_NORMAL;
		this._okImageSelectedPath		=kALERT_WINDOW_OK_SELECTED;
		this._cancelImageNormalPath	 	=kALERT_WINDOW_CANCEL_NORMAL;
		this._cancelImageSelectedPath	=kALERT_WINDOW_CANCEL_SELECTED;
		
		
		// setting Default type
		this._type=kALERT_WINDOW_TYPE;
	
		
		// setting default colors
		this._buttonsColor="FFD700";
		this._titleColor  ="FFFF00";
		this._messageColor ="996600";
		
		// setting frame of this view
		this.w = Core.Capabilities.getScreenWidth();
		this.h = Core.Capabilities.getScreenHeight();
		
		this.setFrame([0,0,this.h,this.w]);
		
		this.setOnLoad(this.loadView.bind(this));
	},
	
	loadView: function()
	 {
	 	var frame=null;
	 	//var x = ((this.h - 356 )/2) - (356/2.4);
		//var y = ((this.w - 229 )/2) - (229/2.75);
        
        var x = 62;
        var y = 45;
										 
		//console.log("Background Path: "+this._backgroundImagePath);
		this._backgroundImage = new UI.Image();
		this._backgroundImage.setImage(this._backgroundImagePath);
		//this._backgroundImage.setImage("asdalsjdaksjd");
		this._backgroundImage.setImageFit(UI.ViewGeometry.FitModes.Stretch);
		this._backgroundImage.setFrame([this.getAdjustedY(x),this.getAdjustedX(y),this.adjustHeightWithDevice(356),this.adjustWidthWithDevice(229)]);
		this.addChild(this._backgroundImage);
		
			
			frame=[this.getAdjustedY(x+135),this.getAdjustedX(y+145),this.adjustHeightWithDevice(73),this.adjustWidthWithDevice(37)];
			this._okButton = UIFactory.getGenericButton(frame, this._okImageNormalPath, this._okImageSelectedPath, this._otherButtonTitles,this._buttonsColor,14,this,null);
			this._okButton.setOnClick(this.clickedButtonOk.bind(this));
			this.addChild(this._okButton);
		
		if( this._type=="okwithcancel")
		{

			frame=[this.getAdjustedY(x+90),this.getAdjustedX(y+145),this.adjustHeightWithDevice(73),this.adjustWidthWithDevice(37)];
			this._okButton.setFrame(frame);
			
			frame=[this.getAdjustedY(x+180),this.getAdjustedX(y+145),this.adjustHeightWithDevice(73),this.adjustWidthWithDevice(37)];
			this._cancelButton = UIFactory.getGenericButton(frame, this._cancelImageNormalPath, this._cancelImageSelectedPath, this.cancelButtonTitle,this._buttonsColor,14,this,null);
			this._cancelButton.setOnClick(this.clickedButtonCancel.bind(this));
			this.addChild(this._cancelButton);
			
		}
		
			this._messageLabel = new UI.Label();
            this._messageLabel.setFrame([this.getAdjustedY(x+25),this.getAdjustedX(y+20),this.adjustHeightWithDevice(300),this.adjustWidthWithDevice(120)]);
			this._messageLabel.setTextColor(this._messageColor); 
			this._messageLabel.setTextSize(this.getAdjustedFontSize(75));
			this._messageLabel.setTextGravity(UI.ViewGeometry.Gravity.Center);
			this._messageLabel.setText(this._message);	
			this.addChild(this._messageLabel);
			
			  
			this._titleLabel = new UI.Label();
			this._titleLabel.setFrame([this.getAdjustedY(x+120),this.getAdjustedX(y+190),this.adjustHeightWithDevice(100),this.adjustWidthWithDevice(30)]);
			this._titleLabel.setTextColor(this._titleColor); 
			this._titleLabel.setTextSize(this.getAdjustedFontSize(75));
			this._titleLabel.setTextGravity(UI.ViewGeometry.Gravity.Center);
			this._titleLabel.setText(this._title);	
			this.addChild(this._titleLabel);
		
	},
	
	show: function()
	{
		NGWindow.document.addChild(this);
	},
	
	// on click call backs
	clickedButtonOk: function()
	{
		this.clickedButtonAtIndex(0)		
	 return true;
	},
	
	clickedButtonCancel: function()
	{
		this.clickedButtonAtIndex(1)		
	 return true;	
	},
	
	
	clickedButtonAtIndex: function(buttonIndex)
	{
		
		if( this._delegate)		
		{
			this._delegate.clickedButtonAtIndex(buttonIndex,this);
		}
		
		this.destroy();	
		
	},
	
	////////////////////////////////////////////////////////////
	
	//getter
	getTagValue: function()
	{
		return this._tagValue;
	},
	
	
	//setters
	
	setTagValue: function(value)
	{
		this._tagValue = value;
	},
	
	setType : function(type)
	{
		this._type=type;
	},
	setButtonColor : function(color)
	{
		this._buttonsColor=color;
		
	},
	
	setTitleColor : function(color)
	{
		this._titleColor=color;
	},
	
	setMessageColor : function(color)
	{
		this._messageColor=color;
	},

	setBackgroundImagePath: function(path)
	{
		this._backgroundImagePath =path;
	},
	
	setOkImageNormalPath: function(path)
	{
		this._okImageNormalPath =path;
	},
	
	setOkImageSelectedPath: function(path)
	{
		this._okImageSelectedPath =path;
	},
	
	setCancelImageNormalPath: function(path)
	{
		this._cancelImageNormalPath =path;
	},
	
	setCancelImageSelectedPath: function(path)
	{
		this._cancelImageSelectedPath =path;
	},
		
	////////////////////////////////////////////////////////////
	
	getAdjustedX: function(x) {
   		
   		var width = 0 , newWidth = 0 ;
   		width = Core.Capabilities.getScreenWidth();
   		newWidth = (width / 320) * x;
   		
       	return newWidth;
   		
   },
    
   getAdjustedY: function(y) 
   {
   		var height = 0 , newHeight = 0;
		height = Core.Capabilities.getScreenHeight();
		newHeight = (height / 480) * y;
      
       return newHeight;
   },
    
	adjustWidthWithDevice: function(aWidth)
	{
		var w = 0, calWidth = 0;
		
		w = Core.Capabilities.getScreenWidth();
		
		calWidth = aWidth * (w/320);
		//////NgLogE('Bandri Width:'+aWidth +'-->'+calWidth +'device W:'+w);
		return calWidth;			
	},
	adjustHeightWithDevice: function(aHeight)
	{
		var h = 0 , calHeight = 0;
		
		h = Core.Capabilities.getScreenHeight();
		
		calHeight = aHeight * (h/480);
		//////NgLogE('Bandri Width:'+aHeight +'-->'+calHeight +'device H:'+h);
		return calHeight;		
		
	},
	
	getAdjustedFontSize: function(fieldHeight) {
        return this.getAdjustedX(fieldHeight / 2) / aspectRatioFactor.d;
    },
	////////////////////////////////////////////////////////////
	
	destroy: function($super)
	{
		// removing from NGWindow
		//NGWindow.document.removeChild(this);
		
		// removing childs from this view and destroying them
		
		if(this._titleLabel!=null)
		{
			this.removeChild(this._titleLabel);
			this._titleLabel.destroy();
			this._titleLabel=null;
		}
		
		if(this._messageLabel!=null)
		{
		this.removeChild(this._messageLabel);
		this._messageLabel.destroy();
		this._messageLabel=null;
		}
		
		if(this._cancelButton!=null)
		{
		this.removeChild(this._cancelButton);
		this._cancelButton.destroy();
		this._cancelButton=null;
		}
		
		if(this._backgroundImage!=null)
		{
		this.removeChild(this._backgroundImage);
		this._backgroundImage.destroy();
		this._backgroundImage=null;
		}
		
		
		if( this._okButton != null)
		{	
			this.removeChild(this._okButton);
			this._okButton.destroy();
			this._okButton=null;
		}
	
		
		$super();
		
	},
	
	
});